package com.qiyue.admin.auth.service.impl;

import com.qiyue.admin.auth.service.AuthService;
import com.qiyue.admin.auth.utils.jwt.utils.JwtTokenUtil;
import com.qiyue.admin.auth.utils.model.impl.JWTInfo;
import com.qiyue.admin.rpc.service.PermissionService;
import com.qiyue.api.vo.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2018/2/10.
 * Description :
 */
@Service
public class AuthServiceImpl implements AuthService {

    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private PermissionService permissionService;


    @Autowired
    public AuthServiceImpl(
            JwtTokenUtil jwtTokenUtil,
            PermissionService permissionService) {
        this.jwtTokenUtil = jwtTokenUtil;
        this.permissionService = permissionService;
    }

    @Override
    public String login(String account, String password) throws Exception {
        UserInfo info = permissionService.validate(account,password);
        String token = "";
        if (!StringUtils.isEmpty(info.getId())) {
            token = jwtTokenUtil.generateToken(new JWTInfo(info.getAccount(), info.getId() , info.getName()));
        }
        return token;
    }

    @Override
    public void validate(String token) throws Exception {
        jwtTokenUtil.getInfoFromToken(token);
    }

    @Override
    public Boolean invalid(String token) {
        // TODO: 2017/9/11 注销token
        return null;
    }

    @Override
    public String refresh(String oldToken) {
        // TODO: 2017/9/11 刷新token
        return null;
    }
}
